package com.lg.patter.behavior.observed.ex.myex;

import java.util.ArrayList;
import java.util.List;

/**
 * @author by Mr. Li 2020/5/8 22:24
 */
public class WeatherData implements Subject {

    private List<Observer> observers;
    private String weather;

    public WeatherData() {
        observers = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    /**
     * 唤醒所有 观察者
     */
    @Override
    public void notifyObserver() {
        for (int i = 0; i < observers.size(); i++) {
            Observer observer = observers.get(i);
            observer.update(weather);
        }
    }

    public void setWeather(String weather) {
        this.weather = weather;
        // 数据更新，通知观察者
        notifyObserver();
    }
}
